package com.chazz.servlet;

import com.chazz.entity.User;
import com.chazz.service.UserService;
import com.chazz.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    private static UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码
        req.setCharacterEncoding("UTF-8");
        //获取参数
        String email = req.getParameter("email");
        String password=req.getParameter("password");
        User user=null;
        PrintWriter out=resp.getWriter();
        try {
            user=userService.findUserByEmail(email);
            if(user!=null&&password.equals(user.getPassword())){
                HttpSession session=req.getSession();
                session.setAttribute("user",user);
//                System.out.println("登录成功");
                resp.sendRedirect("index.jsp");
            }else{
                out.write("true");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
